package com.jianZhiOffer;

import java.util.Arrays;

public class Solution2_55 {
    public boolean isBalanced(TreeNode root) {
        return getDeepth(root) != -1;
    }

    public int getDeepth(TreeNode root) {
        if (root == null) return 0;
        int left = getDeepth(root.left);
        int right = getDeepth(root.right);
        if (left == -1 || right == -1) return -1;
        if (Math.abs(left - right) > 1) return -1;
        return 1 + Math.max(left, right);
    }
}
